#!/bin/bash
# 构建 Deepin/UOS 玲珑包

set -e

# 配置参数
APP_ID="mirror-tool"
VERSION=$(python3 -c "import core; print(core.__version__)")
ARCH="amd64"
OUTPUT_DIR="../build/uab"

# 检查 ll-cli
if ! command -v ll-cli &> /dev/null; then
    echo "需要安装玲珑包管理器"
    exit 1
fi

# 创建临时目录
BUILD_DIR=".uab-build"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR/opt/apps/$APP_ID"

# 复制文件
cp -r core plugins data "$BUILD_DIR/opt/apps/$APP_ID"
cp flatpak/mirror-tool.desktop "$BUILD_DIR/opt/apps/$APP_ID/"

# 生成元数据
cat > "$BUILD_DIR/opt/apps/$APP_ID/info.json" <<EOF
{
    "appid": "$APP_ID",
    "name": "Mirror Tool",
    "version": "$VERSION",
    "arch": ["$ARCH"],
    "permissions": {
        "system-files": ["/etc/apt/sources.list"]
    }
}
EOF

# 构建包
ll-cli pack "$BUILD_DIR" -o "$OUTPUT_DIR" --arch "$ARCH"

echo "构建成功: $OUTPUT_DIR/${APP_ID}_${VERSION}_${ARCH}.uab"